//
//  HPNConfig.m
//  Network
//
//  Created by phq on 2019/7/4.
//  Copyright © 2019 phq. All rights reserved.
//

#import "HPNConfig.h"

@implementation HPNConfig

- (instancetype)init {
    self = [super init];
    if (self) {
        _requestMethod = RequestMethodGet;
        [self setupManager];
        
    }
    return self;
}

// 配置默认的Manager
- (void)setupManager {
    self.manager = [AFHTTPSessionManager manager];
    self.responseDataType = ResponseDataTypeSerializer;
    self.timeoutInterval = 25.f;
}

// 响应数据解释类型
- (void)setResponseDataType:(ResponseDataType)responseDataType {
    _responseDataType = responseDataType;
    
    if (responseDataType == ResponseDataTypeJSON) {
        _manager.responseSerializer = [AFJSONResponseSerializer serializer];
    }else if (responseDataType == ResponseDataTypeXML) {
        _manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
    }else if (responseDataType == ResponseDataTypeSerializer) {
        _manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        _manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    }
}

// 设置超时时间
- (void)setTimeoutInterval:(NSTimeInterval)timeoutInterval {
    if (timeoutInterval <= 0) return;
    _timeoutInterval = timeoutInterval;
    
    [_manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    _manager.requestSerializer.timeoutInterval = timeoutInterval;
    [_manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
}

@end
